const query = require('../models');


module.exports = {
  get:(req,res)=>{
    let logId=req.query.logId;
    console.log('接收到的数据',logId)
    const sql = 'SELECT user_nickname nickname ,user_email email  ,user_phone phone , avator , department_id deptId FROM admin_users JOIN department USING (department_id) WHERE user_id=?';
    query(sql, [logId])
      .then((result)=>{
        console.log('请求成功')
        res.json({
          code:'200',
          mes:'查询成功',
          data: result[0]
        })
      })
      .catch((err) => {
        res.json({
          code: '200',
          mes: '查询失败'
        })
      })
  },
  getdept:(req,res)=>{
    const sql = 'SELECT department_id deptId, department_name deptName FROM department;';
    query(sql,null)
      .then((result) => {
        console.log('请求成功')
        res.json({
          code: '200',
          mes: '查询成功',
          data: result
        })
      })
      .catch((err) => {
        res.json({
          code: '200',
          mes: '查询失败'
        })
      })
  },
  edit:(req,res)=>{
    console.log('接收到编辑的数据', req.body);
    data = req.body;
    inputVal = [data.nikenameVal, data.emialVal, data.phoneVal, data.deptVal, data.passwordValB, data.newAvator,data.logId];
    console.log('转换后',inputVal)
    const sql = 'UPDATE admin_users SET user_nickname = ?, user_email = ?, user_phone = ?,department_id=?, user_pass = ? ,avator=? WHERE user_id =?';
    query(sql,inputVal)
      .then((result)=>{
        console.log('编辑成功');
        res.json({
          code:'200',
          msg:'编辑成功',
        })
      })
      .catch((err) => {
        console.log('操作失败');
        res.json({
          code: '1002',
          msg: '编辑失败',
        })
      })
  },
  uploadavator:(req,res,next)=>{
    console.log('上传文件时传递的其他文本信息', req.body);
    console.log('上传文件时传递的其他文本信息', req.file)
    const fileUrl = '127.0.0.1:3000/my-uploads/' + req.file.filename
    res.json({
      msg: '成功',
      data: {
        url: fileUrl
      }
    });
  }
}